package pers.qianyu.month_202101.date_20210117;

import org.junit.*;

/**
 * 38. 外观数列
 * https://leetcode-cn.com/problems/count-and-say/
 *
 * @author mizzle rain
 * @date 2021-01-17 18:00
 */
public class CountAndSay {
    public String countAndSay(int n) {
        StringBuilder cur = new StringBuilder("1");
        for (int i = 2; i <= n; i++) {
            char[] chars = cur.toString().toCharArray();
            cur = new StringBuilder();
            int count = 1;
            char ch = chars[0];
            for (int j = 1; j < chars.length; j++) {
                if (chars[j] == chars[j - 1]) {
                    count++;
                } else {
                    cur.append(count).append(ch);
                    count = 1;
                    ch = chars[j];
                }
            }
            cur.append(count).append(ch);
        }
        return cur.toString();
    }

    @Test
    public void test1() {
        String result = new CountAndSay().countAndSay(5);
        System.out.println("result = " + result);
    }
}
